<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单例子</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body,
        #app {
            width: 100%;
            height: 100%;
        }

        /* 头部样式 */
        header {
            height: 48px;
            line-height: 48px;
            text-align: center;
            background-color: #545c64;
        }

        header h1 {
            font-weight: 400;
            font-size: 24px;
            color: #fff;
        }

        /* 尾部样式 */
        .footer {
            width: 100%;
            background-color: #545c64;
            color: #fff;
            height: 40px;
            line-height: 40px;
            text-align: center;
            position: absolute;
            bottom: 0;
        }

        /* 主体样式 */
        .main {
            width: 100%;
            position: absolute;
            top: 45px;
            bottom: 40px;
            display: flex;
        }

        .left,
        .right {
            flex: 1;
            text-align: center;
        }

        .left {
            flex: 0 0 20%;
            background-color: #545c64;
            color: #fff;
        }

        .left li {
            list-style: none;
            height: 50px;
            line-height: 50px;
            border-bottom: 1px solid #fff;
            background-color: #a0a0a0;
        }

        .left li a {
            text-decoration: none;
            color: #fff;
            cursor: pointer;
            display: block;
        }

        .right {
            margin-top: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table td,
        table th {
            line-height: 35px;
            font-size: 12px;
            border: 1px solid #eee;
        }

        table th {
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <!-- main instance -->
    <div id="app">
        <header>
            <h1>某公司后台管理系统</h1>
        </header>
        <div class="main">
            <div class="left">
                <ul>
                    <li>
                        <router-link to="/user">用户管理</router-link>
                    </li>
                    <li>
                        <router-link to="/goods">商品管理</router-link>
                    </li>
                    <li>
                        <router-link to="/rights">权限管理</router-link>
                    </li>
                </ul>
            </div>
            <div class="right">
                <router-view></router-view>
            </div>
        </div>
        <div class="footer">版权所有&copy;</div>
    </div>

    <!-- 引用顺序不能乱 -->
    <script src="../Vue.js"></script>
    <script src="../VueRouter.js"></script>
    <script>
        // 创建组件
        const User = {
            template: `
            <!-- user manage -->
            <div id="user">
                <h3>用户管理</h3>
                <table>
                    <thead>
                        <tr>
                            <th>编号</th>
                            <th>姓名</th>
                            <th>年龄</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="item in users" :key="item.id">
                            <td>{{item.id}}</td>
                            <td>{{item.name}}</td>
                            <td>{{item.age}}</td>
                            <td>查看详情</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            `,
            data() {
                return {
                    users: [
                        {
                            id: 1,
                            name: 'zhang san',
                            age: 23
                        },
                        {
                            id: 2,
                            name: 'li si ',
                            age: 23
                        },
                        {
                            id: 3,
                            name: 'wang wu',
                            age: 23
                        }
                    ]
                }
            }
        }
        const Goods = {
            template: `
            <!-- goods manage -->
            <div id="goods">
                <h3>商品管理</h3>
            </div>
            `
        }
        const Rights = {
            template: `
            <!-- rights manage -->
            <div id="rights">
                <h3>权限管理</h3>
            </div>
            `
        }
        // 创建路由
        const router = new VueRouter({
            routes: [
                {
                    path: '/user',
                    component: User,
                    children:[
                        {
                            path:'info',
                            component:{
                                template:`
                                    <div>
                                        <h3>这里是子路由</h3>
                                        <h4>子路由中的path，可以写相对路径，也可以写绝对路径。</h4>
                                        <h4>相对路径就是现在这样，绝对路径，就要以斜杠/开头，但是绝对路径要包含父路径</h4>
                                    </div>
                                `
                            }
                        }
                    ]
                },
                {
                    path: '/goods',
                    component: Goods
                },
                {
                    path: '/rights',
                    component: Rights
                },
                {
                    path: '/',
                    redirect: '/user'
                },
                {
                    // 所有的都匹配不到的时候，走这个。优先显示对应的路径
                    path:'*',
                    component : {
                        template:`<h1>404 Not Found</h1>`
                    }
                }
            ]
        })
        const vm = new Vue({
            el: '#app',
            router
        })
    </script>
</body>

</html>